package com.useeinfo.oa.modules.wuHanCommon.base.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.oa.modules.wuHanCommon.base.dao.WuHanUnitDao;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WuHanUnit;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WuHanUnitBiz extends CrudBiz<WuHanUnitDao, WuHanUnit> {

    public Boolean exist(WuHanUnit wuHanUnit) {
        if (StringUtils.isBlank(wuHanUnit.getUnit())) {
            return true;
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("unit", wuHanUnit.getUnit());
        List<WuHanUnit> wuHanUnits = findList(queryParam);

        if (wuHanUnit.getId() == null) {
            return CollectionUtils.isNotEmpty(wuHanUnits);
        } else {
            return !((wuHanUnits.size() == 1 && wuHanUnits.get(0).getId().equals(wuHanUnit.getId())) || wuHanUnits.size() == 0);
        }
    }

    public WuHanUnit getWuHanUnit(String unit) {
        if (StringUtils.isBlank(unit)) {
            return null;
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("unit", unit);
        List<WuHanUnit> wuHanUnits = findList(queryParam);
        if (CollectionUtils.isNotEmpty(wuHanUnits)) {
            return wuHanUnits.get(0);
        }
        return null;
    }

}
